$(function(){
	if(User.id > 0 && User.id !=''){
		Popup.userTagPopup("a[data-tip]");
	}
});
var Popup = {
		popup_settimeout : null,//settimeout的id，用来取消事件的执行
		popobj:null,
		clearTimeOut:function(){
			if(Popup.popup_settimeout)
				clearTimeout(Popup.popup_settimeout);
		},
		/**
		 * 鼠标经过感兴趣的用户头像、名字、话题效果
		 */
		userTagPopup:function(triggerItems) {
			if($("#bubble").html()!=null){
				Popup.popobj = $("#bubble");
			}else{
				Popup.popobj = $('<div id="bubble"></div>');
			}
			
			// $(triggerItems).hover(
			$(triggerItems).unbind("mouseover");
			$(triggerItems).unbind("mouseout");
			$(triggerItems).bind("mouseover",function(e) {
				var v = $(this).attr("data-tip");
				var vs = v.split("$");
				var trigger = this;
				var u = "p";
				if (vs[0] == "p")
					u = "p";
				else if(vs[0] == "t")
					u = "t";
				
				Popup.clearTimeOut();
				Popup.popup_settimeout = setTimeout(function(){
					Popup.loadPopupInfo(trigger, u, vs[1],e);
				},300);
			});
			
			$(triggerItems).bind("mouseout",function() {
				//var $xqk = this.popobj;
				Popup.clearTimeOut();
				var flag_popup = true;
				//延迟半秒隐藏
				Popup.popup_settimeout = setTimeout(function(){
					if(flag_popup)
						Popup.popobj.css("display", "none");
				},500);
				
				Popup.popobj.mouseover(function() {
					flag_popup = false;//鼠标经过pop，取消隐藏
					Popup.popobj.css("display", "block");
					Popup.clearTimeOut();
				});
				Popup.popobj.mouseout(function() {
					flag_popup = true;//鼠标离开pop，执行隐藏
					Popup.popobj.css("display", "none");
					Popup.clearTimeOut();
				});
				
			});
		},
		/**
		 * 加载user，tag 提示信息
		 */
		loadPopupInfo:function(trigger, u, id,e) {
			// var e = window.event?window.event:arguments.callee.caller.arguments[0];
			Wenda.post(basePath+"json/searchJsonAction!utPopup.action", {
				"flag" : u,"utId":id
			},
			function(data) {
				var pophtml = "";
				if (u == "p"){
					pophtml = Popup.popupuser(data.user);
				}else{
					pophtml = Popup.popuptag(data.tag);
				}
				Popup.popobj.html(pophtml);
				if($("#bubble").html()==null){
					$("body").append(Popup.popobj);
				}
				Popup.popobj.show();
				
				var clientY = e.clientY;
				var clientX = e.clientX;
				var remainWidth = document.body.clientWidth;//body显示区的宽度
				var trigger_width = $(trigger).width();//出发者的宽度
				var popup_width = Popup.popobj.width();
				
				var left = $(trigger).offset().left;
				var top = $(trigger).offset().top;
				var height = 0 ;//与浏览器上边的高度
				if($(trigger).find("img").height()){//如果是图片
				  height = $(trigger).find("img").offset().top-$(window).scrollTop();
				}else{
					height =$(trigger).offset().top-$(window).scrollTop();
				}
				
				
				//计算left   beg
				if((remainWidth - left) > popup_width){//右边宽度大于popup的跨度
					left = left - 20;
					Popup.popobj.find("div[class*='b_arrow']").css("left",30);
				}else{
					left = left - popup_width + trigger_width;
					Popup.popobj.find("div[class*='b_arrow']").css("left",popup_width-30);
				}
				//计算left   end
				
				//计算top	beg
				if(height > Popup.popobj.height()){
					if($(trigger).find("img").height()){
						top = top - Popup.popobj.height() - $(trigger).find("img").height();
					}else{
						top = top - Popup.popobj.height() - $(trigger).height();
					}
					Popup.popobj.css("left", left + "px");
					Popup.popobj.css("top", top + "px");
					
					Popup.popobj.find("div[class='b_arrow_up']").hide();
					Popup.popobj.find("div[class='b_arrow']").addClass("b_arrow");
				}else{
					if($(trigger).find("img").height()){
						top = $(trigger).find("img").offset().top + $(trigger).find("img").height();
					}else{
						top = top + $(trigger).height();
					}
					top = top + 8;
					Popup.popobj.css("left", left + "px");
					Popup.popobj.css("top",  top + "px");
					Popup.popobj.find("div[class='b_arrow_up']").addClass("b_arrow_up");
					Popup.popobj.find("div[class='b_arrow']").hide();
				}
				//计算top	end
				
				 // popup关注按钮事件
				Follow.popupFollowButtonEvent();
				DzqFriend.reqFriend("#bubble a[data-name='req-friend']");
				DzqFriend.remFriend("#bubble a[data-name='rem-friend']");
			}, 'json');
			// }
		},
		
		/**
		 * 鼠标悬浮在user上时弹出的popup
		 * @param uname
		 * @param uhref
		 * @param uehref
		 * @param ac
		 * @param fc
		 * @param desc
		 * @param imgpath
		 */
		popupuser:function(data) {
			
			var popupHtml = Popup.getPopupHtml().replace(/\{user_id\}/gi, data.id).replace(/\{user_name\}/gi, (data.name==null?"":data.name))
						.replace(/\{base_url\}/gi, Constans.BASE_PATH).replace(/\{user_img_url\}/gi, Constans.IMG_BASE_URL+"m_"+data.img)
						.replace(/\{user_Level\}/gi, Popup.user_Level(data.type)).replace(/\{user_address\}/gi, (data.city==null?"":data.city))
						.replace(/\{fs\}/gi, data.fc).replace(/\{cf\}/gi, data.cf).replace(/\{frc\}/gi, data.frc).replace(/\{hc\}/gi, data.apc)
						.replace(/\{sc\}/gi, data.sc).replace(/\{ask\}/gi, data.askc).replace(/\{ans\}/gi, data.ansc).replace(/\{pos\}/gi, (data.pos==null?"":data.pos))
						.replace(/\{detail\}/gi, (data.detail==null?"":data.detail)).replace(/\{company\}/gi, (data.company==null?"":data.company));
			var obj = $(popupHtml);
			
			if(User.id == data.id){
				obj.find("div[id='add_friend']").parent().hide();
			}else{
				if(data.everyCanSendMsg){
					obj.find("div[id='msg_private']").show();
				}else{
					obj.find("div[id='msg_private']").hide();
				}
				obj.find("div[id='add_friend']").hide();
				obj.find("div[id='del_friend']").hide();
				obj.find("div[id='req_friend']").hide();
		 
				
				if(data.goodFriend == 1){ //they are friends
				
					obj.find("div[id='del_friend']").show();
					obj.find("div[id='msg_private']").show();
					
					
				}else if(data.goodFriend == 2){ //req sended 
					obj.find("div[id='req_friend']").show();
					
				}else{ //stranger
					obj.find("div[id='add_friend']").show();
					 
					
				}
				if(data.currentUserFollows == 1){
					data["goodFriend"] == 1&&$(obj.find("div[id='del_follow']")).find("text").text("互相关注");
					data["goodFriend"] == 0&&$(obj.find("div[id='del_follow']")).find("text").text("已关注");
					obj.find("div[id='add_follow']").hide();
					obj.find("div[id='del_follow']").show();
				}else{
					obj.find("div[id='del_follow']").hide();
					obj.find("div[id='add_follow']").show();
				}
				
				var os= obj.find("div[id='onlineStatus']");
				os.attr("class", data.onlineStatus ?"online":"offline");
				os.attr("title", data.onlineStatus ?"当前在线":"离线");
				os.html(data.onlineStatus ?"在线":"离线");
				
			}
			
			
			return 	obj;		
		},
		/**
		 * 鼠标悬浮在tag上时弹出的popup
		 */
		popuptag:function(data) {
			
			var popupHtml = Popup.getTagPopupHtml().replace(/\{user_id\}/gi, data.id).replace(/\{user_name\}/gi, (data.name==null?"":data.name))
						.replace(/\{base_url\}/gi, Constans.BASE_PATH).replace(/\{user_img_url\}/gi, Constans.IMG_BASE_URL+"m_"+data.img)
						.replace(/\{detail\}/gi, (data.detail==null?"":data.detail)).replace(/\{follow_counts\}/gi, data.fc).replace(/\{question_counts\}/gi, data.qc);
			var obj = $(popupHtml);
			
			if(data.currentUserFlowed == 1){
				obj.find("div[id='add_follow']").hide();
				obj.find("div[id='del_follow']").show();
			}else{
				obj.find("div[id='add_follow']").show();
				obj.find("div[id='del_follow']").hide();
			}
			
			
			return 	obj;		
		},
		user_Level:function(type){
			if(type == 4 || type == 5 || type == 3){
				return '<span title="电子圈个人认证"><img class="authentic-small" src="'+Constans.IMG_BASE_URL+'authentication.gif"/></span><font class="aut-desc">( 个人认证 )</font>';
			}else if(type == 1){
				return '<span title="电子圈名人认证"><img class="expert-small" src="'+Constans.IMG_BASE_URL+'authentication.gif"/></span><font class="aut-desc">( 名人认证 )</font>';
			}
			else if(type == 2){
				return '<span title="电子圈企业认证"><img class="firm-small" src="'+Constans.IMG_BASE_URL+'authentication.gif"/></span><font class="aut-desc">( 企业认证 )</font>';
			}
			else return "";
		},
		getPopupHtml:function(){
			var popup_Html = ''//'<div id="bubble">'
				+'<div class="b_arrow_up"></div><div class="b_info">'
				+	'<div class="b_icon"><a href="{base_url}user/userAction!getUser.action?currentId={user_id}"><img src="{user_img_url}" width="50" height="50" /></a></div>'
				+  '<div class="b_key">'
				+  	'	<p class="b_name"><a href="{base_url}user/userAction!getUser.action?currentId={user_id}">{user_name}</a>{user_Level}</p>'
				+      '<div id="onlineStatus"></div>'
				+      '<p class="b_adress">{user_address}</p>'
				+      '<p class="b_attention"><a href="javascript:;" onclick="Popup.links.follows({user_id})" class="b_blue" >关注</a>{cf}<span class="b_line">|</span>'
				+'		<a href="javascript:;" onclick="Popup.links.fans({user_id})" class="b_blue">粉丝</a>{fs}<span class="b_line">|</span>'
				+'		<a href="javascript:;" onclick="Popup.links.friends({user_id})" class="b_blue">好友</a>{frc}</p>'
				+  '</div>'
				+'</div>'
				+'<div class="b_title">{pos} {company}</div>'
				+'<div class="b_summary">{detail}</div>'
				+'<div class="b_partyNum">'
				+'	<a href="javascript:;" onclick="Popup.links.partys({user_id})" class="b_blue">活动</a>{hc}<span class="b_line">|</span>'
				+'	<a href="javascript:;" onclick="Popup.links.shares({user_id})" class="b_blue">分享</a>{sc}<span class="b_line">|</span>'
				+'	<a href="javascript:;" onclick="Popup.links.asks({user_id})" class="b_blue">提问</a>{ask}<span class="b_line">|</span>'
				+'	<a href="javascript:;" onclick="Popup.links.answers({user_id})" class="b_blue">回答</a>{ans}</div>'
				+'<div class="b_postDiv" id="b_postDiv">'
				+'	<div class="b_attented" id="del_follow"><text>已关注</text><span class="b_line">|</span><a href="javascript:;" class="b_blue" data-name="popup" id="pf-{user_id}">取消</a></div>'
//				+'	<div class="b_attented" id="del_friend">互为好友<span class="b_line">|</span><a href="javascript:;" class="b_blue" data-name="rem-friend" data-area="popup" data-uid="{user_id}">取消</a></div>'
//				+'	<div class="b_attented" id="req_friend">已发好友请求</div>'
				+'	<div class="b_add_attention fl" id="add_follow"><a href="javascript:;" data-name="popup" id="pf-{user_id}">加关注</a></div>'
//				+'	<div class="b_add_friend fl" id="add_friend"><a href="javascript:;" data-name="req-friend" data-area="popup" data-uid="{user_id}">加好友</a></div>'
				+'	<div class="b_send_message fl" id="msg_private" onclick="getMsgDiv(\'\',{user_id},\'{user_name}\');"><a href="javascript:;" >私信聊天</a></div>'
				+'</div><div class="b_arrow"></div>';
			return popup_Html;
		},
		links : {
			fans:function(uid){//粉丝
				window.location.href=Constans.BASE_PATH+'user/userAction!getUser.action?flag=beFollow&currentId='+uid;
			},
			asks:function(uid){//提问
				window.location.href=Constans.BASE_PATH+'user/userAction!getUser.action?flag=asked&currentId='+uid;
			},
			answers:function(uid){//回答
				window.location.href=Constans.BASE_PATH+'user/userAction!getUser.action?flag=answered&currentId='+uid;
			},
			follows:function(uid){//关注
				window.location.href=Constans.BASE_PATH+'user/userAction!getUser.action?flag=follow&currentId='+uid;
			},
			partys:function(uid){//活动
				window.location.href=Constans.BASE_PATH+"user/userAction!getUser.action?flag=party&currentId="+uid;
			},
			friends:function(uid){//好友
				window.location.href=Constans.BASE_PATH+"user/userAction!getUser.action?flag=friends&currentId="+uid;
			},
			shares:function(uid){//分享
				window.location.href=Constans.BASE_PATH+"user/userAction!getUser.action?flag=share&currentId="+uid;
			}
		},
		getTagPopupHtml:function(){
			var popup_Html = ''
				+'<div class="b_arrow_up"></div><div class="b_info">'
				+	'<div class="b_icon"><a href="{base_url}tag/tagAction!tagindex.action?tagName={user_name}"><img src="{user_img_url}" width="50" height="50" /></a></div>'
				+  '<div class="b_key">'
				+  	'	<p class="b_name"><a href="{base_url}tag/tagAction!tagindex.action?tagName={user_name}">{user_name}</a></p>'
				+  '</div>'
				+'</div>'
				+'<div class="b_summary">{detail}</div>'

				+'<div class="b_postDiv" id="b_postDiv">'
				+'	<div class="b_attented">已有{question_counts}个问题</div>'
				+'	<div class="b_attented">{follow_counts} 人关注该话题</div>'
				+'	<div class="b_attented" id="del_follow">已关注<span class="b_line">|</span><a href="javascript:;" class="b_blue" data-name="popup" id="tf-{user_name}">取消</a></div>'
				+'	<div class="b_add_attention" id="add_follow"><a href="javascript:;" data-name="popup" id="tf-{user_name}">加关注</a></div>'
				+'</div><div class="b_arrow"></div>';
				//+'</div>';
			return popup_Html;
		}
};


	


